package com.qiuzhiyi.activity;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.utils.ScreenUtil;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.AddressAdapter;
import com.qiuzhiyi.adapter.MoreJobAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.AddressItem;
import com.qiuzhiyi.item.MoreJobItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Li
 * Date: 2017/11/23 0023
 * Description: 找工作--更多
 */

public class MoreJobActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.txt_address)
    TextView mTxtAddress;
    @BindView(R.id.img_address)
    ImageView mImgAddress;
    @BindView(R.id.layout_address)
    LinearLayout mLayoutAddress;
    @BindView(R.id.txt_order)
    TextView mTxtOrder;
    @BindView(R.id.img_order)
    ImageView mImgOrder;
    @BindView(R.id.layout_order)
    LinearLayout mLayoutOrder;
    @BindView(R.id.txt_condition)
    TextView mTxtCondition;
    @BindView(R.id.img_condition)
    ImageView mImgCondition;
    @BindView(R.id.layout_condition)
    LinearLayout mLayoutCondition;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;

    private PopupWindow mPopAddress;

    private RecyclerView mRecyclerLeft,mRecyclerRight;
    private List<AddressItem> mAddressItemsLeft,mAddressItemsRight;
    private AddressAdapter mAdapterLeft,mAdapterRight;


    private MoreJobAdapter mMoreJobAdapter;
    private List<MoreJobItem> mMoreJobItemList;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_more_job;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mAdapterLeft=new AddressAdapter(R.layout.item_address);
        mAdapterRight=new AddressAdapter(R.layout.item_address);
        mAddressItemsLeft=new ArrayList<>();
        mAddressItemsRight=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mAddressItemsLeft.add(new AddressItem());
            mAddressItemsRight.add(new AddressItem());
        }

        mMoreJobItemList=new ArrayList<>();
        mMoreJobAdapter=new MoreJobAdapter(R.layout.item_more_job);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mMoreJobAdapter.bindToRecyclerView(mRecycler);

        for (int i = 0; i < 10; i++) {
            mMoreJobItemList.add(new MoreJobItem());
        }
        mMoreJobAdapter.setNewData(mMoreJobItemList);
        mMoreJobAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                start(JobDetailActivity.class);
            }
        });
    }

    @OnClick({R.id.layout_address, R.id.layout_order, R.id.layout_condition})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_address:
                showPop(view);
                break;
            case R.id.layout_order:
                showPop(view);
                break;
            case R.id.layout_condition:
                showPop(view);
                break;
        }
    }

    private void showPop(final View view){
        view.setBackgroundColor(Color.WHITE);
        mImgAddress.setImageResource(R.drawable.xiangshang);
        if (mPopAddress == null) {
            int[] inWindow=new int[2];
            view.getLocationInWindow(inWindow);
            View content=LayoutInflater.from(mContext).inflate(R.layout.pop_address,null);
            mRecyclerLeft=content.findViewById(R.id.recycler_left);
            mRecyclerRight=content.findViewById(R.id.recycler_right);
            mRecyclerLeft.setLayoutManager(new LinearLayoutManager(mContext));
            mRecyclerRight.setLayoutManager(new LinearLayoutManager(mContext));

            mAdapterLeft.bindToRecyclerView(mRecyclerLeft);
            mAdapterRight.bindToRecyclerView(mRecyclerRight);

            mAdapterLeft.setNewData(mAddressItemsLeft);
            mAdapterRight.setNewData(mAddressItemsRight);

            mAdapterRight.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    for (AddressItem addressItem : mAddressItemsRight) {
                        addressItem.setSelected(false);
                    }
                    mAddressItemsRight.get(position).setSelected(true);
                    mAdapterRight.notifyDataSetChanged();
                    mPopAddress.dismiss();
                }
            });

            mAdapterLeft.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    for (AddressItem addressItem : mAddressItemsLeft) {
                        addressItem.setSelected(false);
                    }
                    mAddressItemsLeft.get(position).setSelected(true);
                    mAdapterLeft.notifyDataSetChanged();
                }
            });

            int height= ScreenUtil.getScreenHeight(mContext)
                    -inWindow[1]-(view.getBottom()-view.getTop());
            mPopAddress=new PopupWindow(content,-1, height);
            mPopAddress.setOutsideTouchable(true);
            mPopAddress.setFocusable(true);
            content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPopAddress.dismiss();
                }
            });

        }
        mPopAddress.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                view.setBackgroundResource(R.color.background);
                mImgAddress.setImageResource(R.drawable.xiangxia);
            }
        });
        mPopAddress.showAsDropDown(view);
    }
}
